﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

namespace DOAN.DLL
{
    public class ClsConnection
    {
        SqlConnection con;
        SqlCommand comand;
        SqlDataAdapter adapter;
        DataTable dt;
        string main;

        public ClsConnection()
        {
            con = new SqlConnection();
            main = ConfigurationManager.ConnectionStrings["DOAN.GUI.Properties.Settings.QUANLYINTERNETConnectionString"].ConnectionString;
           
            con.ConnectionString = main;

            Open();
        }

        private void Open()
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
            }
            catch (SqlException)
            {
                MessageBox.Show("Không thể kết nối!");
                con.Close();
            }
        }

        private void Close()
        {
            con.Close();
            con.Dispose();
        }

        public DataTable GetData(string sql)
        {
            dt = new DataTable();
            try
            {
                comand = new SqlCommand(sql, con);
                //comand.CommandTimeout = 99999999;
                comand.CommandType = CommandType.StoredProcedure;
                adapter = new SqlDataAdapter(comand);
                adapter.Fill(dt);
                comand.Dispose();
                adapter.Dispose();
            }
            catch (SqlException)
            {
                MessageBox.Show("Không thể load dữ liệu!");
                con.Close();
            }
            return dt;
        }

        public DataTable GetData(string sql, string[] name, object[] value, int Nparameter)
        {
            dt = new DataTable();
            try
            {
                comand = new SqlCommand(sql, con);
                for (int i = 0; i < Nparameter; i++)
                {
                    comand.Parameters.AddWithValue(name[i], value[i]);
                }
                //comand.CommandTimeout = 99999999;
                comand.CommandType = CommandType.StoredProcedure;
                adapter = new SqlDataAdapter(comand);
                adapter.Fill(dt);
                comand.Dispose();
                adapter.Dispose();
            }
            catch (SqlException)
            {
                MessageBox.Show("Không thể load dữ liệu!");
                con.Close();
            }
            return dt;
        }

        public int UpdateData(string sql)
        {
            int ex = 0;
            try
            {
                comand = new SqlCommand(sql, con);
                //comand.CommandTimeout = 999999999;
                comand.CommandType = CommandType.StoredProcedure;
                ex = comand.ExecuteNonQuery();
                comand.Dispose();
            }
            catch (SqlException)
            {
                MessageBox.Show("Không thể cập nhật dữ liệu!");
                con.Close();
            }
            return ex;
        }

        public int UpdateData(string sql, string[] name, object[] value, int Nparameter)
        {
            int ex = 0;
            try
            {
                comand = new SqlCommand(sql, con);
                for (int i = 0; i < Nparameter; i++)
                {
                    comand.Parameters.AddWithValue(name[i], value[i]);
                }
                //comand.CommandTimeout = 999999999;
                comand.CommandType = CommandType.StoredProcedure;
                ex = comand.ExecuteNonQuery();
                comand.Dispose();
            }
            catch (SqlException e)
            {
                MessageBox.Show("Không thể cập nhật dữ liệu!");
                con.Close();
            }
            return ex;
        }

        public int AutoUpdateDepartment(string sql, DataTable dt)
        {
            int ex = 0;
            try
            {
                comand = new SqlCommand(sql, con);
                adapter = new SqlDataAdapter(comand);
                SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
                adapter.Update(dt);
                ex = 1;
            }
            catch (SqlException e)
            {
                //MessageBox.Show(e.ToString());
                con.Close();
            }
            return ex;
        }
    }
}
